package com.yuanchu.goods.listener;

import com.alibaba.fastjson.JSON;
import com.yuanchu.goods.entity.YoshopGoodsEntity;
import com.yuanchu.goods.service.YoshopGoodsService;
import com.yuanchu.goods.service.YoshopGoodsSkuService;
import com.yuanchu.messages.service.MqMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

import static com.yuanchu.goods.config.DearMessageConfig.PAYNOTIFY_GOODS_QUEUE;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-02-23 17:08
 */
@Slf4j
@Component
public class OrderListener {

    @Resource
    private YoshopGoodsSkuService goodsSkuService;
    @Resource
    private YoshopGoodsService goodsService;

    @Resource
    private MqMessageService mqMessageService;

    // @RabbitListener(bindings = @QueueBinding(
    //         value = @Queue(name = PAYNOTIFY_GOODS_QUEUE),
    //         exchange = @Exchange(name = "paynotify_exchange_direct"),
    //         key = {"goods"}
    // ))
    //监听消息队列接收支付结果通知
    @RabbitListener(queues = PAYNOTIFY_GOODS_QUEUE)
    public void listenSimpleQueue(String msg) {
        // 接收到消息
        log.info("消费者接收到simple.queue的消息：【{}】", msg);
        Map map = JSON.parseObject(msg, Map.class);
        String goodsId = map.get("goodsId").toString();
        String goodsSkuId = map.get("goodsSkuId").toString();
        String goodsNum = map.get("goodsSkuId").toString();
        boolean decrStockNum = goodsSkuService.decrStockNum(Integer.parseInt(goodsId), goodsSkuId, Integer.parseInt(goodsNum));
        if (!decrStockNum) {
            log.info("库存减少失败！");
            // throw new YuanChuException("库存减少失败！");
            mqMessageService.addMessage("库存减少失败！", goodsId, goodsSkuId, goodsNum);
            return;
        }
        YoshopGoodsEntity goods = goodsService.getById(goodsId);
        if (goods == null){
            log.info("商品不存在！");
            mqMessageService.addMessage("商品不存在！", goodsId, goodsSkuId, goodsNum);
            return;
        }
        if (goods.getStockTotal() < goods.getStockTotal() - Integer.parseInt(goodsNum)) {
            log.info("库存不足！");
            mqMessageService.addMessage("库存不足！", goodsId, goodsSkuId, goodsNum);
            return;
        }
        goods.setStockTotal(goods.getStockTotal() - Integer.parseInt(goodsNum));
        goods.setSalesActual(goods.getSalesInitial() + Integer.parseInt(goodsNum));
        goodsService.updateById(goods);
        log.info("消息处理完成！");
    }


    //监听死信队列
    // @RabbitListener(queues = DL_GOODS_QUEUE)
    // public void listenDlQueue(String msg) {
    //     // 接收到消息
    //     log.info("消费者接收到死信队列dl_goods_queue的消息：【{}】", msg);
    //     Map map = JSON.parseObject(msg, Map.class);
    //     String goodsId = map.get("goodsId").toString();
    //     String goodsSkuId = map.get("goodsSkuId").toString();
    //     String goodsNum = map.get("goodsSkuId").toString();
    //     log.info("记录死信消息！");
    //     mqMessageService.addMessage("decrStockNum", goodsId, goodsSkuId, goodsNum);
    //     log.info("消息处理完成！");
    // }


}
